﻿using Company.Database.Entities;
using Microsoft.EntityFrameworkCore;


namespace Company.Database.Sqlite
{
    public class SqliteDbContext :DbContext
    {
       public DbSet<UserEntity> Users { get; set; }
       public DbSet<AlarmEntity> Alarms { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);

            //设置sqlite数据库的路径
            string dbPath = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), 
                this.GetType().Name,
                "database2.db");//C:\Users\host\AppData\Local\SqliteDbContext\database2.db
            //创建路径
            Directory.CreateDirectory(Path.GetDirectoryName(dbPath)!);
            //使用sqlite数据库
            optionsBuilder.UseSqlite($"Data Source={dbPath}");
            //将数据库路径写入到dbpath.txt文件中,方便调试
            File.WriteAllText("dbpath.txt", dbPath);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            Database.Migrate();//自动迁移数据库
        }
    }
}
